package com.smyh.model.host.param;

import android.content.Context;
import android.graphics.Color;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.smyh.UIM.host.param.HardwareParam;
import com.smyh.bean.HostHardwareInfo;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.model.tableModel.KtableView;
import com.smyh.protcol.host.HostCMDUtil;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;

/**
 * Created by 姜龙 on 2016/7/8.
 */
public class HardWareModel {

    private static TableLayout tableLayout;

    public static TableLayout getTableLayout(Context context){
        if(tableLayout == null){
            tableLayout = new TableLayout(context);
            tableLayout.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
            tableLayout.setBackgroundColor(Color.BLACK);
            KtableView.setIsEditColNum(1);
            String header0[] = {"硬件参数"};
            String header1[] = {"名称","值","描述"};
            String test1[] = {"指示器组数","","1字节"};
            String test2[] = {"电池报警阀值","","1字节"};
            String test3[] = {"GSM模块关机电压","","1字节"};
            String test4[] = {"GSM模块开机电压","","1字节"};
            String test5[] = {"重连次数","","1字节"};
            String test6[] = {"最小重连间隔","","2字节"};
            String test7[] = {"遥测上传周期","","2字节"};
            String test8[] = {"心跳周期","","2字节"};
            String test9[] = {"运行模式","","1字节"};
            tableLayout.addView(KtableView.addTableHeaders(context,header0));
            tableLayout.addView(KtableView.addTableHeaders(context,header1));
            tableLayout.addView(KtableView.addNewRow(context,test1));
            tableLayout.addView(KtableView.addNewRow(context,test2));
            tableLayout.addView(KtableView.addNewRow(context,test3));
            tableLayout.addView(KtableView.addNewRow(context,test4));
            tableLayout.addView(KtableView.addNewRow(context,test5));
            tableLayout.addView(KtableView.addNewRow(context,test6));
            tableLayout.addView(KtableView.addNewRow(context,test7));
            tableLayout.addView(KtableView.addNewRow(context,test8));
            tableLayout.addView(KtableView.addNewRow(context,test9));
        }
        return tableLayout;
    }

    public static IoBuffer writeParam(){
        //硬件参数修改
        IoBuffer buf = IoBuffer.allocate(0).setAutoExpand(true);
        for (int j = 2; j < tableLayout.getChildCount(); j++) {
            TableRow childRow = (TableRow) tableLayout.getChildAt(j);
            switch (j) {
                case 2: {
                    //指示器组数
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(Integer.toHexString(cell));
                    byte[] bt = new byte[1];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }
                case 3: {
                    //电池报警阀值
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(Integer.toHexString(cell));
                    byte[] bt = new byte[1];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }
                case 4: {
                    //GSM关机电压
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(Integer.toHexString(cell));
                    byte[] bt = new byte[1];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }
                case 5: {
                    //GSM开机电压
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(Integer.toHexString(cell));
                    byte[] bt = new byte[1];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }
                case 6: {
                    //重连次数
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(Integer.toHexString(cell));
                    byte[] bt = new byte[1];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }
                case 7: {
                    //最小重连间隔
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(String.format("%04x", cell));
                    byte[] bt = new byte[2];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }
                case 8: {
                    //遥测上传周期
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(String.format("%04x", cell));
                    byte[] bt = new byte[2];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }
                case 9: {
                    //心跳周期
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(String.format("%04x", cell));
                    byte[] bt = new byte[2];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }

                case 10: {
                    //运行模式
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(Integer.toHexString(cell));
                    byte[] bt = new byte[1];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }
            }
        }
        byte[] bTemp = new byte[48];
        buf.put(bTemp);

        buf.flip();
        IoBuffer buf1 = HostCMDUtil.writeParam(2, buf,(byte)0);
        return buf1;
    }

    public static void setTableLayout() {

        for (int i = 2; i < tableLayout.getChildCount(); i++) {
            TableRow childRow = (TableRow) tableLayout.getChildAt(i);
            HostHardwareInfo info = HardwareParam.hardwareInfoMap.get(MainCahce.chooseDeviceAddr);
            if (info != null){
                switch (i) {
                    case 2:
                        ((TextView) childRow.getChildAt(1)).setText(info.getGroupNum() + "");
                        break;
                    case 3:
                        ((TextView) childRow.getChildAt(1)).setText(info.getBatteryWarn() + "");
                        break;
                    case 4:
                        ((TextView) childRow.getChildAt(1)).setText(info.getGsmTurnOffVolt() + "");
                        break;
                    case 5:
                        ((TextView) childRow.getChildAt(1)).setText(info.getGsmTurnOnVolt() + "");
                        break;
                    case 6:
                        ((TextView) childRow.getChildAt(1)).setText(info.getRepeatConnect() + "");
                        break;
                    case 7:
                        ((TextView) childRow.getChildAt(1)).setText(info.getRepeatConnectMinInter() + "");
                        break;
                    case 8:
                        ((TextView) childRow.getChildAt(1)).setText(info.getMockCycle() + "");
                        break;
                    case 9:
                        ((TextView) childRow.getChildAt(1)).setText(info.getHeartCycle() + "");
                        break;
                    case 10:
                        ((TextView) childRow.getChildAt(1)).setText(info.getRunStyle() + "");
                        break;

                }

            }else {
                switch (i) {
                    case 2:
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        break;
                    case 3:
                        ((TextView) childRow.getChildAt(1)).setText( " ");
                        break;
                    case 4:
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        break;
                    case 5:
                        ((TextView) childRow.getChildAt(1)).setText( " ");
                        break;
                    case 6:
                        ((TextView) childRow.getChildAt(1)).setText( " ");
                        break;
                    case 7:
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        break;
                    case 8:
                        ((TextView) childRow.getChildAt(1)).setText( " ");
                        break;
                    case 9:
                        ((TextView) childRow.getChildAt(1)).setText( " ");
                        break;
                    case 10:
                        ((TextView) childRow.getChildAt(1)).setText( " ");
                        break;

                }

            }
        }
    }

}
